package com.scuola.scuoladocente;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.text.Html;


public class VotoAlunno extends Activity {
	
	private ListView lv;
	private JSONArray parsedList = null;
	private JSONParser parser = new JSONParser();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voto_alunno);
        
        TextView tv_header = (TextView)findViewById(R.id.tv_va_header);
        tv_header.setText(Html.fromHtml("<b>Studente: </b>"+getIntent().getStringExtra("cognome")+" "+getIntent().getStringExtra("nome")
        		+"<br/><b>Materia: </b>"+getIntent().getStringExtra("nomeMateria")));
        
        lv = (ListView)findViewById(R.id.lv_va_voti);
        ArrayList<String> lvItems = new ArrayList<String>();
        
        try {
        	AsyncTask<Void, Void, String> getAlumniByClassIdResponse = new getVoto().execute();
			parsedList = (JSONArray) parser.parse(getAlumniByClassIdResponse.get());
			
			for(int i = 0; i < parsedList.size(); i++ ){
				JSONObject obj = (JSONObject) parser.parse(parsedList.get(i).toString());
				lvItems.add("Tipo: "+obj.get("tipologia").toString()+"\t"+obj.get("data").toString()+"\nVoto: "+obj.get("voto").toString());
			}
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvItems);
			lv.setAdapter(adapter);
			
	    }catch (Exception e) {
			e.printStackTrace();
		}
        
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					JSONObject selectedObj = (JSONObject) parser.parse(parsedList.get(arg2).toString());
					Intent InserisciVoto = new Intent(getApplicationContext(), InserisciVoto.class);
					InserisciVoto.putExtra("classeId", selectedObj.get("classeId").toString());
					InserisciVoto.putExtra("materiaId", selectedObj.get("materiaId").toString());
					InserisciVoto.putExtra("studenteId", selectedObj.get("studenteId").toString());
					InserisciVoto.putExtra("docenteId", selectedObj.get("docenteId").toString());
					InserisciVoto.putExtra("data", selectedObj.get("data").toString());
					InserisciVoto.putExtra("voto", selectedObj.get("voto").toString());
					InserisciVoto.putExtra("tipologia", selectedObj.get("tipologia").toString());
					InserisciVoto.putExtra("votoId", selectedObj.get("idVoto").toString());
					startActivityForResult(InserisciVoto, 0);
					finish();
		           	
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
    }
    
    private class getVoto extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... arg0) {
			try{	
				LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
				hm.put("studenteId", getIntent().getStringExtra("idAlunno"));
				hm.put("materiaId", getIntent().getStringExtra("idMateria"));
				String strResponse = operations.wsCall("getVoto", hm).toString();
				return strResponse;
			} catch (Exception e) {
				System.err.println("getMaterieByDocenteId errore (Exception)");
				e.printStackTrace();
				return null;
			}
		}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_voto_alunno, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.crea_voto:
            	Intent InserisciVoto = new Intent(getApplicationContext(), InserisciVoto.class);
            	InserisciVoto.putExtra("classeId", getIntent().getStringExtra("idClasse"));
				InserisciVoto.putExtra("materiaId", getIntent().getStringExtra("idMateria"));
				InserisciVoto.putExtra("studenteId", getIntent().getStringExtra("idAlunno"));
				InserisciVoto.putExtra("docenteId", getIntent().getStringExtra("idDocente"));
            	startActivityForResult(InserisciVoto, 0);
            	finish();
                break;
        }
		return true;
    }
 
}
